/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server.persistencia;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.dominio.Horario;
import server.dominio.Medico;



/**
 *
 * @author tpaxle
 */
public class DAOHoraMySQL extends DAOHora{
    private Agente Broker;
    private Connection Con;
    private DateFormat formatear;
    private DateFormat formatearn, formatearh;
    public DAOHoraMySQL() throws SQLException{
            Broker= Agente.getAgente();
            Con= Broker.getConnection();
            formatear= new SimpleDateFormat("yyyy-MM-dd");
            formatearn= new SimpleDateFormat("yyy-MM-dd HH:mm");
            formatearh= new SimpleDateFormat("HH:mm");
    }
    @Override
    public ArrayList<Object> select(String condicion) throws SQLException {
        ArrayList<Object> listaHora= new ArrayList<Object>();
        Statement stmt;

        DAOMedicoMySQL daoM= new DAOMedicoMySQL();

        stmt = (Statement) Con.createStatement();
        ResultSet res  = stmt.executeQuery(condicion);

         while(res.next()) {
             java.util.Date dia= null, horadesde= null, horahasta = null;
            try {
                horadesde= formatearh.parse(res.getString(3));
                horahasta= formatearh.parse(res.getString(4));
                dia = formatear.parse(res.getString(5));
     
            } catch (ParseException ex) {
                Logger.getLogger(DAOHoraMySQL.class.getName()).log(Level.SEVERE, null, ex);
            }

             Medico medico=(Medico) daoM.find("SELECT * FROM Medico Where idMedico_pk= "+res.getInt("id_medico_fk"));             
             Horario hora= new Horario(medico, horadesde, horahasta, dia, res.getString(6), null);
             hora.setIdHorario(res.getInt(1));
             listaHora.add(hora);
         }
         res.close();
         stmt.close();
         daoM.cerrarDAO();
         this.cerrarDAO();

        return listaHora;
    }

    @Override
    public int insert(Object obj) throws SQLException {
        Horario hora_aux= (Horario)obj;
        int resul=0;      
        /*La persona no se inserta aki Debe estar ya insertada*/

        PreparedStatement stmt = (PreparedStatement) Con.prepareStatement("INSERT INTO horario(id_Medico_fk, Desde, Hasta, dia, estado) VALUES (?,?,?,?,?)");

        stmt.setInt(1, hora_aux.getMedico().getIdMedicoPk());
        System.out.println(formatear.format(hora_aux.getDia()) + " " + formatearh.format(hora_aux.getDesde())+ " "+ formatearh.format(hora_aux.getHasta()));
        stmt.setString(4, formatear.format(hora_aux.getDia()));
        stmt.setString(2, formatearh.format(hora_aux.getDesde()));
        stmt.setString(3, formatearh.format(hora_aux.getHasta()));
        stmt.setString(5, "libre");
        resul= stmt.executeUpdate();

         stmt.close();

        return resul;
    }

    @Override
    public int update(Object obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int delete(Object hora) throws SQLException {
            PreparedStatement stmt;
            int result= 0;
            stmt = (PreparedStatement) Con.prepareStatement("DELETE FROM Horario WHERE id_Horario= ?");
            stmt.setInt(1, ((Horario)hora).getIdHorario());
            result= stmt.executeUpdate();

            return result;
    }

    @Override
    public Object find(String condicion) throws SQLException {
        Horario hora= null;
	Statement stmt;
        DAOMedicoMySQL daom= new DAOMedicoMySQL();
	stmt = (Statement) Con.createStatement();
	ResultSet res  = stmt.executeQuery(condicion);

        if(res.next()){
                Medico medico= (Medico) daom.find("SELECT * FROM Medico WHERE idMedico_pk= "+res.getInt("id_Medico_fk"));
                if(medico!= null){
             
                    java.util.Date dia= null, horadesde= null, horahasta = null;
                    try {
                        horadesde= formatearh.parse(res.getString(3));
                        horahasta= formatearh.parse(res.getString(4));
                        dia = formatear.parse(res.getString(5));
                    } catch (ParseException ex) {
                        Logger.getLogger(DAOHoraMySQL.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    hora= new Horario(medico, horadesde, horahasta, dia, res.getString(6), null);
                    hora.setIdHorario(res.getInt(1));

             
                }

        }
        daom.cerrarDAO();
        return hora;
    }

    @Override
    public void cerrarDAO() throws SQLException {
        Con.close();
    }

}
